package cn.netuo.bumbersoll.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;

import com.rooten.util.Util;

import lib.grasp.util.L;

import static android.content.Context.BATTERY_SERVICE;

/**
 * Created by JS_grasp on 2019/3/18.
 */
public class BatteryHelper extends BroadcastReceiver {
    /**
     * 是否正在充电
     */
    private boolean mIsCharging = false;

    /**
     * 剩余电量(0-100)
     */
    private int mPowerLeft = 100;

    /**
     * 获取设备剩余电量百分比
     */
    public int getBatteryLeftRate(Context context) {
        L.logOnly("查询剩余电量: " + String.valueOf(mPowerLeft));
        return mPowerLeft;
    }

    /**
     * 是否正在充电
     */
    public boolean isBatteryCharging(Context context) {
        L.logOnly("查询充电状态: " + (mIsCharging ? "是" : "否"));
        return mIsCharging;
    }

    /** 主动检查剩余电量 */
    private void checkBatteryLeftRate(Context context){
        int cap = 0;
        BatteryManager manager = (BatteryManager) context.getSystemService(BATTERY_SERVICE);
        if (manager != null && Build.VERSION.SDK_INT >= 26) {    // 8.0
            cap = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);///当前电量百分比
        } else if (manager != null && Build.VERSION.SDK_INT >= 21) {    // 5.0
            cap = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);///当前电量百分比
        }
        L.logOnly("主动查询剩余电量: 当前电量: " + cap);
        if(cap == 0) return;
        mPowerLeft = cap;
    }

    /** 主动检查是否正在充电 */
    private boolean checkBatteryCharging(Context context){
        boolean isCharging = false;
        BatteryManager manager = (BatteryManager) context.getSystemService(BATTERY_SERVICE);
        if (manager != null && Build.VERSION.SDK_INT >= 26) {    // 8.0
            isCharging = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS) == BatteryManager.BATTERY_STATUS_CHARGING;
        } else if (manager != null && Build.VERSION.SDK_INT >= 21) {    // 5.0
            isCharging = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS) == BatteryManager.BATTERY_STATUS_CHARGING;
        }
        L.logOnly("主动查询充电状态: " + (isCharging ? "是" : "否"));
        mIsCharging = isCharging;
        return isCharging;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        assert action != null;
        switch (action){
            case Intent.ACTION_BATTERY_CHANGED: {   // 系统电量变化
                int powerLeft = Util.getInt(intent, BatteryManager.EXTRA_LEVEL, -1);
                if(powerLeft != -1){
                    mPowerLeft = powerLeft;
                }
                else{
                    checkBatteryLeftRate(context);
                }

                int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
                mIsCharging = ((status == BatteryManager.BATTERY_STATUS_CHARGING) || checkBatteryCharging(context));

                L.logOnly("电量监听结果:当前电量: " + String.valueOf(mPowerLeft) + ", 正在充电: " + (mIsCharging ? "是" : "否"));
                break;
            }
        }
    }

    /** 获取蓝牙动态广播的监听 */
    public static IntentFilter getIntentFilter() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        return intentFilter;
    }
}
